package initialize

import (
	"os"

	"github.com/test-instructor/yangfan/server/model/interfacecase"

	"github.com/test-instructor/yangfan/server/global"
	"github.com/test-instructor/yangfan/server/model/example"
	"github.com/test-instructor/yangfan/server/model/system"

	"go.uber.org/zap"
	"gorm.io/gorm"
)

// Gorm 初始化数据库并产生数据库全局变量
// Author SliverHorn
func Gorm() *gorm.DB {
	switch global.GVA_CONFIG.System.DbType {
	case "mysql":
		return GormMysql()
	case "pgsql":
		return GormPgSql()
	default:
		return GormMysql()
	}
}

// RegisterTables 注册数据库表专用
// Author SliverHorn
func RegisterTables(db *gorm.DB) {
	err := db.AutoMigrate(
		// 系统模块表
		system.SysApi{},
		system.SysUser{},
		system.SysBaseMenu{},
		system.JwtBlacklist{},
		system.SysAuthority{},
		system.SysDictionary{},
		system.SysOperationRecord{},
		system.SysAutoCodeHistory{},
		system.SysDictionaryDetail{},
		system.SysBaseMenuParameter{},
		system.SysBaseMenuBtn{},
		system.SysAuthorityBtn{},
		system.SysAutoCode{},

		// 示例模块表
		example.ExaFile{},
		example.ExaCustomer{},
		example.ExaFileChunk{},
		example.ExaFileUploadAndDownload{},

		// 接口测试模块
		system.SysUserProject{},
		system.Project{},
		interfacecase.ApiMenu{},
		interfacecase.ApiStepThinkTime{},
		interfacecase.ApiStepRendezvous{},
		interfacecase.ApiStepTransaction{},
		interfacecase.ApiStep{},
		interfacecase.ApiGrpc{},
		interfacecase.ApiRequest{},
		interfacecase.ApiConfig{},
		interfacecase.ApiCaseStep{},
		interfacecase.ApiCaseStepRelationship{},
		interfacecase.ApiDebugTalk{},
		interfacecase.ApiCase{},
		interfacecase.ApiCaseRelationship{},
		interfacecase.ApiUserConfig{},

		interfacecase.HrpPyPkg{},

		//环境变量
		interfacecase.ApiEnv{},
		interfacecase.ApiEnvDetail{},

		// 性能测试模块
		interfacecase.Performance{},
		interfacecase.PerformanceReport{},
		interfacecase.PerformanceReportDetail{},
		interfacecase.PerformanceRelationship{},
		interfacecase.PerformanceReportTotalStats{},
		interfacecase.PerformanceReportMaster{},
		interfacecase.PerformanceReportWork{},
		interfacecase.PerformanceReportWorker{},

		//测试报告模块
		interfacecase.ApiReport{},
		interfacecase.ApiReportStat{},
		interfacecase.ApiReportTime{},
		interfacecase.ApiReportStatTestcases{},
		interfacecase.ApiReportStatTeststeps{},
		interfacecase.ApiReportDetails{},
		interfacecase.ApiReportDetailsRecords{},
		interfacecase.ApiReportDetailsRecordsData{},
		interfacecase.ApiReportDetailsRecordsDataHttpstat{},

		//定时任务
		interfacecase.ApiTimerTask{},
		interfacecase.ApiTimerTaskTag{},
		interfacecase.ApiTimerTaskRelationship{},
		interfacecase.ApiTimerTaskTagRelationship{},

		//消息通知
		interfacecase.ApiMessage{},
		interfacecase.ApiMessageLog{},

		//CI记录
		interfacecase.ApiReportCI{},

		// 自动化模块表
		// Code generated by github.com/flipped-aurora/gin-vue-admin/server Begin; DO NOT EDIT.

		// Code generated by github.com/flipped-aurora/gin-vue-admin/server End; DO NOT EDIT.
	)
	if err != nil {
		global.GVA_LOG.Error("register table failed", zap.Error(err))
		os.Exit(0)
	}
	global.GVA_LOG.Info("register table success")
}
